package com.dctp.common.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class PnlService {


        /**
     * @param prev  上一帧的 orderId->pnl (可从 Redis / 本地 Map 取)
     * @param now   本帧 freshly 计算的 orderId->pnl
     * @param minTickDiff  触发推送的最小变化（如 0.01 USDT）
     * @return      仅包含“变化超过阈值”的订单 diff；若 diff 为空可跳过推送
     */
    public static Map<String, BigDecimal> buildDiff(
            Map<Long, BigDecimal> prev,
            Map<Long, BigDecimal> now,
            BigDecimal minTickDiff) {

        Map<String, BigDecimal> diff = new HashMap<>();
        now.forEach((id, pnlNow) -> {
            BigDecimal pnlPrev = prev.get(id);
            if (pnlPrev == null || pnlNow.subtract(pnlPrev).abs().compareTo(minTickDiff) >= 0) {
                diff.put(id.toString(), pnlNow);
            }
        });
        return diff;
    }
}
